<?php
namespace data\sys\power;
use \data\BaseData;
/**
 * 菜单数据对象
 */
class MenuData extends BaseData{
    
    /**
     * 读取一个菜单信息
     */
    public function find($param){
        $fields = ['id','plat','menuurl','pid','status'];
        $where = $this->parseWhere($fields,$param);
        if( !$where )  dataE('查询条件不能为空');
        $info  = M('sys_power_menu')->where($where)->find();
        return $info;
    }
    
    /**
     * 列表读取
     */
    public function getList($param){
        $page = $param['page']?:1;
        $limit= $param['limit']?:20;        
        $check = ['plat'=>2,'status'=> 1,'pid', 'depth'];
        $where = $this->parseWhere($check,$param);
        $order = 'sort desc,id desc';
        $list  = M("sys_power_menu")->order($order)->where($where)->page($page)->limit($limit)->select();
        $total = M('sys_power_menu')->where($where)->count();
        return [            
            'page' => $page,
            'limit' => $limit,
            'total'=> $total,
            'list' => $list,
        ];
    }
    
    /**
     * 添加一个菜单信息
     */
    public function add($data){
        $check = [
            'plat'      => ['check'=>'must','msg'=> '平台代号不能为空','reg'=>'int'],
            'menuurl'   => '',            
            'pid'           => ['reg'=>'int'],
            'status'        => ['def'=>3,'reg'=>'int'],
            'depth'         => ['reg'=>[1,2,3,4]],
            'menuname'      => '',
            'remark'        => '',
            'icon'          => '',
            'sort'          => '',
        ];
        $save = $this->verifyData($check,$data);
        return M('sys_power_menu')->insertGetId($save);
        
    }
    
    /**
     * 修改一个菜单信息
     */
    public function edit($id,$data){        
        $check = [
            //'plat'      => ['check'=>'must','msg'=> '平台代号不能为空','reg'=>'int'],
            //'menuurl'   => '',            
            'pid'           => ['reg'=>'int'],
            'status'        => ['reg'=>'int'],
            'depth'         => ['reg'=>[1,2,3,4]],
            'menuname'      => '',
            'remark'        => '',
            'icon'          => '',
            'sort'          => '',
        ];        
        $save = $this->verifyData($check,$data);
        $where['id'] = $id;
        $result = M('sys_power_menu')->where($where)->update($save);
        if( $result === false )  dataE('修改菜单失败');
        return true;
    }
    
    
    
}